from collections import defaultdict, deque
from typing import List


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        fre_counter = defaultdict(int)

        for num in nums:
            fre_counter[num] += 1

        fre = sorted(fre_counter, key=lambda x: fre_counter[x], reverse=True)
        return fre[:k]

print(Solution().topKFrequent([1,2,3,4,5,6,7], 2))

from collections import Counter
import heapq# 小顶堆
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        fre_counter = Counter(nums)

        fre = []
        for value, count in fre_counter.items():
            heapq.heappush(fre,(count,value))
            if len(fre) >k:
                heapq.heappop(fre)
        return [b for a,b in fre]

print(Solution().topKFrequent([1,1,1,2,2,3], 2))